package sk.octopuss.archery.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import sk.octopuss.archery.dao.AbstractGenericDao;
import sk.octopuss.archery.dao.CategoryDao;
import sk.octopuss.archery.domain.Category;

@Repository
public class CategoryDaoImpl extends AbstractGenericDao<Category> implements CategoryDao{

	@Override
	public Category find(final Long id) {
		final Query query = em.createQuery("Select c FROM Category c Where c.id ="+id);
		return (Category) query.getSingleResult();
	}

	@Override
	public List<Category> findAll() {
		final Query query = em.createQuery("Select c FROM Category c ORDER BY c.id ASC");
		return query.getResultList();
	}

	@Override
	public Category findByName(final String name) {
		final Query query = em.createQuery("Select c FROM Category c Where c.name='"+name+'"');
		return (Category) query.getSingleResult();
	}

	@Override
	public List<Category> findAllByName(final String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Category> findAllEntries(final int firstResult, final int maxResults) {
		   return em.createQuery("SELECT c FROM Category c").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
	}

	

	@Override
	public long countAll() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long save(final Category entity) {
		em.persist(entity);
		return entity.getId();
		
	}

	@Override
	public void delete(final Category entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Category> getParentCategories(final Category category) {
		final List<Category> categories = new ArrayList<Category>();
		Category c = category;
		while (c!=null) {
			categories.add(category);
			c=c.getParent();
		}
		return categories;
	}

}
